package com.example.pagingdemo.paging.githubuser;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pagingdemo.databinding.ItemGithubUserBinding;
import com.example.pagingdemo.model.GithubUser;

/**
 * @author: tuyrt7 on 2020/9/9.
 */
public class GithubUserPageListAdapter extends PagedListAdapter<GithubUser,GithubUserPageListAdapter.ViewHolder> {


    public GithubUserPageListAdapter() {
        super(new DiffUtil.ItemCallback<GithubUser>() {
            @Override
            public boolean areItemsTheSame(@NonNull GithubUser oldItem, @NonNull GithubUser newItem) {
                return oldItem.id == newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull GithubUser oldItem, @NonNull GithubUser newItem) {
                return oldItem.equals(newItem);
            }
        });
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemGithubUserBinding binding = ItemGithubUserBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GithubUser item = getItem(position);
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ItemGithubUserBinding mBinding;

        public ViewHolder(@NonNull ItemGithubUserBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void bindData(GithubUser user) {
            mBinding.setUser(user);
        }
    }
}
